package org.rcy.framework.data.datasource;

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Locale;

import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * @author 吴康桥
 * @Description
 * @date 2022/9/28 10:18
 */

@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
	/**
	 * JDBC url of the database.
	 */
	private String url;

	/**
	 * Login user of the database.
	 */
	private String username;

	/**
	 * Login password of the database.
	 */
	private String password;

	/**
	 * the maximum number of connections in the pool
	 */
	private int maxPoolSize = 2;

	/**
	 * the minimum number of idle connections in the pool to maintain
	 */
	private int minIdle = 1;

	/**
	 * 连接超时时间
	 */
	private long connectionTimeout = SECONDS.toMillis(60);

	/**
	 * 是否自动包装关键字
	 */
	private boolean useKeywordWrap;

	/**
	 * test sql for connection validation
	 */
	private String connectionTestQuery;

	/**
	 * 检测连接超时时间
	 */
	private long validationTimeout = SECONDS.toMillis(5);

	/**
	 * 连接生存时间
	 */
	private long maxLifeTime = MINUTES.toMillis(30);

	private int maxLimit; //最大返回行数

	private int maxLogTime; //sql执行maxLogTime时间（单位毫秒）打印出时间，需在设置maxLimit情况下该设置起效

	private boolean registerMbeans; //是否开启jmx监控

	private long maxTransactionTime;//事务持续大于maxTransactionTime后打出日志  0表示不开启

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getMaxPoolSize() {
		return maxPoolSize;
	}

	public void setMaxPoolSize(int maxPoolSize) {
		this.maxPoolSize = maxPoolSize;
	}

	public int getMinIdle() {
		return minIdle;
	}

	public void setMinIdle(int minIdle) {
		this.minIdle = minIdle;
	}

	public String getDatabaseId() {
		String db = url.split(":")[1].toUpperCase(Locale.ENGLISH);
		return db;
	}

	public long getConnectionTimeout() {
		return connectionTimeout;
	}

	public void setConnectionTimeout(long connectionTimeout) {
		this.connectionTimeout = connectionTimeout;
	}

	public boolean isUseKeywordWrap() {
		return useKeywordWrap;
	}

	public void setUseKeywordWrap(boolean useKeywordWrap) {
		this.useKeywordWrap = useKeywordWrap;
	}

	public String getConnectionTestQuery() {
		return connectionTestQuery;
	}

	public void setConnectionTestQuery(String connectionTestQuery) {
		this.connectionTestQuery = connectionTestQuery;
	}

	public long getValidationTimeout() {
		return validationTimeout;
	}

	public void setValidationTimeout(long validationTimeout) {
		this.validationTimeout = validationTimeout;
	}

	public long getMaxLifeTime() {
		return maxLifeTime;
	}

	public void setMaxLifeTime(long maxLifeTime) {
		this.maxLifeTime = maxLifeTime;
	}

	public int getMaxLimit() {
		return maxLimit;
	}

	public void setMaxLimit(int maxLimit) {
		this.maxLimit = maxLimit;
	}

	public int getMaxLogTime() {
		return maxLogTime;
	}

	public void setMaxLogTime(int maxLogTime) {
		this.maxLogTime = maxLogTime;
	}

	public boolean isRegisterMbeans() {
		return registerMbeans;
	}

	public void setRegisterMbeans(boolean registerMbeans) {
		this.registerMbeans = registerMbeans;
	}

	public long getMaxTransactionTime() {
		return maxTransactionTime;
	}

	public void setMaxTransactionTime(long maxTransactionTime) {
		this.maxTransactionTime = maxTransactionTime;
	}

	public String getDbName() {
		if(this.url == null ||"".equals(this.url.trim())) {
			return null;
		}
		try {
			String[] split = url.split(":");
			String[] portSplit = split[3].split("/");
			String[] databaseSplit = portSplit[1].split("\\?");
			return databaseSplit[0].toLowerCase();
		} catch (Exception e) {
			return null;
		}

	}
}
